<template>
  <el-dialog
    class="dialog"
    :title="title"
    :visible.sync="show"
    width="800px"
    
    :before-close="close">
    <el-table
      
      @selection-change="handleSelectionChange"
      :data="tableData3"
      height="350"
      border
      style="width: 100%">
      <el-table-column
        type="selection">
      </el-table-column>
      <el-table-column
        prop="index"
        label="序号"
        width="80">
      </el-table-column>
      <el-table-column
        prop="project_title"
        label="项目名称"
        >
      </el-table-column>
      <el-table-column
        prop="student_name"
        label="申报人"
        width="180">
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="page"
      :page-size="10"
      layout="total, prev, pager, next"
      :total="total">
    </el-pagination>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" @click="close">取 消</el-button>
      <el-button size="mini" type="primary" @click="distribution">确定分配</el-button>
    </span>
  </el-dialog>
</template>

<script type="text/ecmascript-6">
export default {
  props: {
    title: String,
    show: Boolean,
    type: String,
    id: Number,
    project: String
  },
  watch: {
    show: "getdata"
  },
  data() {
    return {
      page: 1,
      total: null,
      tableData3: [],
      projectGroup: []
    };
  },
  methods: {
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page = val;
      this.getdata();
    },
    getdata() {
      if (this.type === "judges") {
        
        this.$http.get(`/judges/allot/list/${this.page}`, {
          params: {
            subject: this.project,
            judgesId: this.id
          }
        }).then(res => {
          console.log(res);
          this.total = res.data.total;
          this.tableData3 = res.data.data.map((item, index) => {
            item.index = `${this.page - 1 > 0 ? this.page - 1 : ''}` + `${index + 1}`;
            return item;
          });
        });
      } else {
        this.$http.get(`/auditor/will/allot/list/${this.page}`).then(res => {
          this.total = res.data.total;
          this.tableData3 = res.data.data.map((item, index) => {
            item.index = `${this.page - 1 > 0 ? this.page - 1 : ''}` + `${index + 1}`;
            return item;
          });
        });
      }
    },

    close() {
      this.$emit("close");
    },
    handleSelectionChange1(val) {
      console.log(val);
      // this.multipleSelection = val;
    },
    handleSelectionChange(val) {
      this.projectGroup = [];
      val.map(item => {
        this.projectGroup.push(item.user_id);
      });
      console.log(this.projectGroup);
    },
    distribution() {
      if (this.projectGroup === []) {
        this.$message.error("请选择项目");
        return;
      }
      if (this.type === "judges") {
        let data = {
          judgesId: this.id,
          projectId: this.projectGroup,
        };
        this.$http.post("/allot/project/to/judges", data).then(res => {
          this.$message.success("为评委分配成功");
          this.getdata();
          this.$emit('updata')
        });
      } else {
        let data = {
          auditorId: this.id,
          projectId: this.projectGroup
        };
        this.$http.post("/allot/project/to/auditor", data).then(res => {
          this.$message.success("为审核员分配成功");
          this.getdata();
          this.$emit('updata')
        });
        
      }
    }
  }
};
</script>

<style scoped lang="scss">
// @import "./index.scss";
</style>
